package com.darknight.ftp.connection.factory;

import com.darknight.ftp.connection.FtpUserConnection;
import com.darknight.ftp.connection.pool.ConnectionPool;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.io.IOException;

/**
 * Created by DarKnight on 2017/7/30.
 */
@Component
public class ConnectionFactory {
    static private final String DEFAULT_FTP_HOST_NAME = "localhost";

    static private String DEFAULT_FTP_PORT = "2121";

    private Boolean defaultConfig;

    private String configuredFtpHostName;

    private String configuredFtpPort;

    private String ftpHostName;

    private Integer ftpPort;

    private ConnectionPool connectionPool = new ConnectionPool();

    public ConnectionFactory() {
//        initConnectionPool();
    }

    public ConnectionFactory(String ftpHostName, Integer ftpPort) {
        this.ftpHostName = ftpHostName;
        this.ftpPort = ftpPort;

//        initConnectionPool();
    }

    public Boolean getDefaultConfig() {
        return defaultConfig;
    }

    @Value("${ftpserver.defaultConfig:true}")
    public void setDefaultConfig(Boolean defaultConfig) {
        this.defaultConfig = defaultConfig;
    }

    public String getConfiguredFtpHostName() {
        return configuredFtpHostName;
    }

    @Value("${ftpserver.ftpHostName:}")
    public void setConfiguredFtpHostName(String configuredFtpHostName) {
        this.configuredFtpHostName = configuredFtpHostName;
    }

    public String getConfiguredFtpPort() {
        return configuredFtpPort;
    }

    @Value("${ftpserver.ftpPort:}")
    public void setConfiguredFtpPort(String configuredFtpPort) {
        this.configuredFtpPort = configuredFtpPort;
    }

    public FtpUserConnection createConnection() {
        return new FtpUserConnection(this.ftpHostName, this.ftpPort);
    }

    public void initConnectionPool() {
        Integer poolSize = this.connectionPool.getPoolSize();

        for(int i = 0; i < poolSize; i++) {
            FtpUserConnection connection = createConnection();

            this.connectionPool.addConnection(connection);
        }
    }

    public FtpUserConnection getConnection(String ftpHostName, Integer ftpPort) {
        Assert.hasText(ftpHostName, "连接地址不能为空");
        Assert.notNull(ftpPort, "连接端口不能为空");

        if(this.connectionPool == null) {
            initConnectionPool();
        }

        if(connectionPool.hasConnection(ftpHostName, ftpPort)) {

            return connectionPool.getConnection(ftpHostName, ftpPort);
        }else {
            FtpUserConnection connection = new FtpUserConnection(ftpHostName, ftpPort);

            connectionPool.addConnection(connection);

            return connection;
        }
    }

    public FtpUserConnection getConnection() {

        return getConnection(this.ftpHostName, this.ftpPort);
    }

    public FtpUserConnection getConnectedConnection(String ftpHostName, Integer ftpPort) throws IOException {
        Assert.hasText(ftpHostName, "连接地址不能为空");
        Assert.notNull(ftpPort, "连接端口不能为空");

        FtpUserConnection connection = getConnection(ftpHostName, ftpPort);

        // 连接FTP服务器
        connection.connectFtpServer();

        return connection;
    }

    public void returnConnection(FtpUserConnection connection) {
        this.connectionPool.addConnection(connection);
    }

    public void releaseAllConnection() throws IOException {
        if(connectionPool.isNotEmptyPool()) {
            this.connectionPool.releaseAllConnection();
        }
    }
}
